﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DLL;
namespace BLL
{
   public class PhieuXuatBLL
    {
        DataServer DB = new DataServer();
        public DataTable Select_All()
        {
            string sql = string.Format("sp_HoaDon_GetByAll");
            DataTable bang = DB.LayDuLieuProc(sql);
            return bang;
        }
        public DataTable SelectALL_MaNv()
        {
            string sql = string.Format("sp_NhanVien_GetByAll");
            DataTable bang = DB.LayDuLieuProc(sql);
            return bang;
        }
        public bool Kiemtra(string Ma)
        {
            string sql = string.Format("sp_KhachHang_Kiemtra");
            SqlParameter paramMNTB = new SqlParameter("@MaKH", Ma);
            DataTable bang = DB.LayDuLieuProc(sql, paramMNTB);
            if (bang.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        public string LayMaKH(string MaHD)
        {
            string MaKH;
            string sql = "select MaKH from HoaDon where MaHD=@MaHD";
            SqlParameter paramMNTB = new SqlParameter("@MaHD",MaHD);
            DataTable bang = DB.LayDulieu(sql, paramMNTB);
            MaKH = bang.Rows[0]["MaKH"].ToString();
            return MaKH;
        }
        public bool Them(PhieuXuatDAL px)
        {
            string sql = string.Format("sp_HoaDon_Insert");

            string MaHD = DLL.CommonFunc.GetPrimaryKey("HoaDon");
            SqlParameter paramMHD = new SqlParameter("@MaHD", MaHD);
            SqlParameter paramTHD = new SqlParameter("@TenHD", px.TenHD);
            SqlParameter paramNL = new SqlParameter("@NgayLap", px.Ngaylap);
            SqlParameter paramMNV = new SqlParameter("@MaNv", px.MaNv);
            SqlParameter paramMKH = new SqlParameter("@MaKH", px.MaKH);
            try
            {
                DB.CapNhatProc(sql, paramMHD, paramTHD, paramNL, paramMNV, paramMKH);
            }
            catch
            {
                return false;
            }
            DLL.CommonFunc.UpdatePrimaryKey("HoaDon");
            return true;
        }
        public bool Sua(PhieuXuatDAL px)
        {
            string sql = string.Format("sp_HoaDon_Update");

            
            SqlParameter paramMHD = new SqlParameter("@MaHD", px.MaHD);
            SqlParameter paramTHD = new SqlParameter("@TenHD", px.TenHD);
            SqlParameter paramNL = new SqlParameter("@NgayLap", px.Ngaylap);
            SqlParameter paramMNV = new SqlParameter("@MaNv", px.MaNv);
            SqlParameter paramMKH = new SqlParameter("@MaKH", px.MaKH);
            try
            {
                DB.CapNhatProc(sql, paramMHD, paramTHD, paramNL, paramMNV, paramMKH);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool Xoa(string MaPX)
        {
            string sql = string.Format("sp_HoaDon_Delete");


            SqlParameter paramMHD = new SqlParameter("@MaHD",MaPX);
         
            try
            {
                DB.CapNhatProc(sql, paramMHD);
            }
            catch
            {
                return false;
            }
            return true;
        }
      
        public DataTable Search(PhieuXuatDAL px)
        {
            string sql = string.Format("sp_HoaDon_Search");
            SqlParameter paramMHD = new SqlParameter("@MaHD", px.MaHD);
            SqlParameter paramTHD = new SqlParameter("@TenHD", px.TenHD);
             SqlParameter paramMNV = new SqlParameter("@MaNV", px.MaNv);
            DataTable bang = DB.LayDuLieuProc(sql, paramMHD, paramTHD,paramMNV);
            return bang;
        }
     
    }
}
